<?php
namespace lib\src\driver\redis;

class base{
	protected static $host = "127.0.0.1";
	protected static $prot = "6379";
	/**
	 * userredis
	 */
	public static function useRedis(){
		$redis = new \Redis();
		return $redis;
	} 
	/**
	 * redis 数据库操作类
	 * 默认db 为0
	 */
	public static function connRedis($host,$prot,$db = 0){
		$redis = self::useRedis();
		$redis->connect(self::$host,self::$prot);
		$redis->auth('');
		$redis->select($db);
		return $redis;
	}
	/**
	 * 存入基本数据信息(string)
	 */
	public static function setString($key,$value,$db ="1"){
		
		$redis = self::connRedis(self::$host, self::$prot,$db);
		$redis->set($key,$value);
	}
	/**
	 * 取出基本数据
	 */
	public static function getString($key,$db="1"){
		
		$redis = self::connRedis(self::$host, self::$prot,$db);
		$row = $redis->get($key);
		return $row;
	}
	/**
	 * 删除基本数据
	 */
	public static function delString($key,$db="1"){
		
		$redis = self::connRedis(self::$host, self::$prot,$db);
		$row = $redis->del($key);
		return $row;
	}
	/**
	 * 将数据存入到哈希中
	 */
	public static function setHash($key,$field,$value,$db){
		
		$redis = self::connRedis(self::$host, self::$prot,$db);
		$row = $redis->hset($key,$field,$value); 
		return $row;
	}
	/**
	 * 将数据从哈希中取出来
	 */
	public static function getHash($key,$field,$db){
		
		$redis = self::connRedis(self::$host, self::$prot,$db);
		$row = $redis->hget($key,$field);
		return $row;
	} 
	/**
	 * 检查哈希表是否存在设定的字段
	 */
	public static function checkHashExist($key,$field,$db){
		
		$redis = self::connRedis(self::$host, self::$prot,$db);
		$row = $redis->hexists($key,$field);
		return $row; 
	}
	/**
	 * 将值存入到list头部
	 */
	public static function setListInHead($key,$value,$db){
		
		$redis = self::connRedis(self::$host, self::$prot,$db);
		$row = $redis->lpush($key,$value);
		return $row;
	}
	/**
	 * 取出list中所有的值
	 */
	public static function getListAll($key,$db){
		
		$redis = self::connRedis(self::$host, self::$prot,$db);
		//获取长度
		$length = $redis->llen($key);
		//获取长度范围内的所有的值
		$row = $redis->lrange($key,0,$length);
		return $row; 
	}
	/**
	 * 删除list中值定的值
	 */
	public static function delListVal($key,$field,$db){
		
		$redis = self::connRedis(self::$host, self::$prot,$db);
		//删除指定的key的值
		$row = $redis->lrem($key,0,$field);
		return $row;
	}
	/**
	 * 设置key的生存时间
	 */
	public static function setStringTll($key,$time,$db = "1"){
		
		$redis = self::connRedis(self::$host, self::$prot,$db);
		//设置指定key的生存时间
		$row = $redis->expire($key,$time);
		return $row;
	}
	/**
	 * 消息发布
	 */
	public static function pushMessage($value,$key){
		$redis = self::connRedis(self::$host, self::$prot,0);
		//发布消息
		$row = $redis->publish($key,$value);
		return $row;
	}
}